package com.boc.langxiang.ui.activity.common;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.ForgetPasswordContract;
import com.boc.langxiang.entity.events.LoginSuccessEvent;
import com.boc.langxiang.entity.response.SmsCodeEntity;
import com.boc.langxiang.presenter.common.ForgetPasswordPresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 找回密码
 */
public class ForgetPassword1Activity extends BaseToolBarActivity implements Handler.Callback
        , ForgetPasswordContract.View {

    @BindView(R.id.phone)
    EditText mPhone;
    @BindView(R.id.send)
    TextView mSend;
    @BindView(R.id.checkcode)
    EditText mCheckcode;
    @BindView(R.id.next)
    TextView mNext;
    private Handler mHandler;
    private ForgetPasswordContract.Presenter mPresenter;
    private int sec;
    private boolean mGetCheckFail;
    private String mInputPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password1);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        setToolBarTitle("找回密碼");

        mHandler = new Handler(this);
        mPresenter = new ForgetPasswordPresenter(this);
    }

    @OnClick({R.id.send, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send:
                mInputPhone = mPhone.getText().toString();
                if (StringUtil.isEmpty(mInputPhone)) {
                    T.showShort(mContext, "请输入手机号码");
                    return;
                }
                sec = 59;
                mGetCheckFail = false;
                mSend.setClickable(false);
                mSend.setText(sec + "s");
                mHandler.sendEmptyMessageDelayed(100, 1000);
                mPresenter.sendCode(mInputPhone);
                break;
            case R.id.next:
                mInputPhone = mPhone.getText().toString();
                if (StringUtil.isEmpty(mInputPhone)) {
                    T.showShort(mContext, "请输入手机号码");
                    return;
                }
                String smsCode = mCheckcode.getText().toString();
                if (TextUtils.isEmpty(smsCode)) {
                    T.showShort(mContext, "请输入短信验证码");
                    return;
                }

                mPresenter.smsCodeVerify(mInputPhone, smsCode);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 100:
                sec--;
                if (mGetCheckFail || sec == 0) {
                    mSend.setText("发送验证码");
                    mSend.setClickable(true);
                    return true;
                }
                mSend.setText(sec + "s");
                mHandler.sendEmptyMessageDelayed(100, 1000);
                return true;
        }
        return false;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        mGetCheckFail = true;
        T.showShort(mContext, msg);
    }

    @Override
    public void onGetSmsCodeSuccess(SmsCodeEntity smsCodeEntity) {
//        T.showShort(mContext, StringUtil.getValue(smsCodeEntity.getMsg()));
    }

    @Override
    public void loginSuccess() {

    }

    @Override
    public void smsCodeVerifySuccess() {
        Intent intent = new Intent(mContext, ForgetPassword2Activity.class);
        intent.putExtra("phone", mInputPhone);
        startActivity(intent);
    }

    @Override
    public void smsCodeVerifyFail(String code, String message) {
        T.showShort(mContext, message);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccessEvent(LoginSuccessEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
